package com.learn.training.multithread.chapter10.demo1;

import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;

public class Demo {

    public static void main(String[] args) {
        // 创建2000个随机数组成的数组:
        long[] array = new long[2000];
        long expectedSum = 0;
        for (int i = 0; i < array.length; i++) {
            array[i] = random();
            expectedSum += array[i];
        }

        System.out.println("expectedSum is : " + expectedSum);

        // fork/join:
        ForkJoinTask<Long> task = new RecursiveTaskExample(array, 0, array.length);
        long startTime = System.currentTimeMillis();
        long result = ForkJoinPool.commonPool().invoke(task);
        long endTime = System.currentTimeMillis();
        System.out.println("Fork/join sum result is : " + result + "  in  " + (endTime - startTime) + " ms");
    }

    static Random random = new Random(0);
    static long random() {return random.nextInt(10000);}
}
